#include "inquirememberprofile.h"
#include "ui_inquirememberprofile.h"
#include "inquiresql.h"
#include <QKeyEvent>

InquireMemberProfile::InquireMemberProfile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InquireMemberProfile)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    setWindowTitle(tr("选择会员"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_selectAllLineEditInquireMemberProfileText = false;

    QHeaderView* vheaderView = ui->tableWidgetMemberProfile->verticalHeader();
    vheaderView->setHidden(true);

    //设置列宽可拖动
//    ui->tableWidgetMemberProfile->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetMemberProfile->verticalHeader()->setResizeMode(QHeaderView::Interactive);
    //设置table的水平滚动条
    ui->tableWidgetMemberProfile->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    ui->tableWidgetMemberProfile->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    QWidget::installEventFilter(this);
    ui->tableWidgetMemberProfile->installEventFilter(this);
    ui->lineEditInquireMemberProfile->installEventFilter(this);
    ui->pushButtonDisplayAll->installEventFilter(this);
}

InquireMemberProfile::~InquireMemberProfile()
{
    delete ui;
}

void InquireMemberProfile::changelineEditInquire(const QString &inquireInformation)
{
    ui->tableWidgetMemberProfile->setFocus();

    if (inquireInformation.isEmpty())
    {
//        ui->lineEditInquireMemberProfile->setText(NULL);
        ui->tableWidgetMemberProfile->clear();
        ui->tableWidgetMemberProfile->setRowCount(0);
    }
    else
        ui->lineEditInquireMemberProfile->setText(inquireInformation);

    setTableWidgetHeaderStyle();
}

bool InquireMemberProfile::eventFilter(QObject *target, QEvent *event)
{
    if( target == this || target == ui->tableWidgetMemberProfile
     || target == ui->lineEditInquireMemberProfile ||target == ui->pushButtonDisplayAll)
    {
        if (event->type() == QEvent::FocusOut)
        {
            if (target == ui->tableWidgetMemberProfile)
            {
                ui->tableWidgetMemberProfile->setStyleSheet("background-color:white");
                return true;
            }
            if (target == ui->lineEditInquireMemberProfile)
            {
                m_selectAllLineEditInquireMemberProfileText = false;
            }
        }
        if (event->type() == QEvent::FocusIn)
        {
            if (target == ui->tableWidgetMemberProfile)
            {
                ui->tableWidgetMemberProfile->setStyleSheet("\
                    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
                    stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
                    stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
                    stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");
                return true;
            }
        }
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->pushButtonDisplayAll)
                {
                    displayMemberProfile("all");
                    return true;
                }
                if (ui->tableWidgetMemberProfile->rowCount())
                    on_tableWidgetMemberProfile_cellDoubleClicked(
                            ui->tableWidgetMemberProfile->currentRow(), 0);
                goto flag;
            }
            if (keyEvent->key() == Qt::Key_F1 || keyEvent->key() == Qt::Key_Tab)
            {
                if (target == ui->tableWidgetMemberProfile)
                {
                    ui->lineEditInquireMemberProfile->setFocus();
                    ui->lineEditInquireMemberProfile->selectAll();
                    return true;
                }
                if (target == ui->lineEditInquireMemberProfile)
                {
                    ui->pushButtonDisplayAll->setFocus();
                    return true;
                }
                if (target == ui->pushButtonDisplayAll)
                {
                    ui->tableWidgetMemberProfile->setFocus();
                    return true;
                }
                else
                {
                    ui->lineEditInquireMemberProfile->setFocus();
                    ui->lineEditInquireMemberProfile->selectAll();
                    return true;
                }
            }
        }
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditInquireMemberProfile)
                {
                    if (!m_selectAllLineEditInquireMemberProfileText){
                        m_selectAllLineEditInquireMemberProfileText = true;
                        ui->lineEditInquireMemberProfile->selectAll();
                        return true;
                    }else if (m_selectAllLineEditInquireMemberProfileText)
                    {
                        goto flag;
                    }
                }
            }
        }
       //窗口停用，变为不活动的窗口
        if(QEvent::WindowDeactivate == event->type())
        {
            this->hide();  //或者关闭窗口,加入自己的操作.
            return true ;
        }
        else
        {
            return false ;
        }
    }
flag:
    return QWidget::eventFilter(target, event);

//    if (event->type() == QEvent::KeyPress)
//    {
//        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
//        if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
//        {
////            qDebug() << "keyEvent";
//            if (ui->tableWidgetMemberProfile->rowCount())
//                on_pushButtonSelete_clicked();
//            return true ;
//        }
//        if (keyEvent->key() == Qt::Key_F1)
//        {
//            if (target == ui->lineEditInquireMemberProfile)
//            {
//                ui->tableWidgetMemberProfile->setFocus();
//                return true;
//            }
//            else
//            {
//                ui->lineEditInquireMemberProfile->setFocus();
//                ui->lineEditInquireMemberProfile->selectAll();
//                return true;
//            }
//        }
//    }
//    if( target == this )
//    {
//       //窗口停用，变为不活动的窗口
//        if(QEvent::WindowDeactivate == event->type())
//        {
////            qDebug() << "into eventFilter";
//            this->hide();  //或者关闭窗口,加入自己的操作.
//            return true ;
//        }
//        else
//        {
//            return false ;
//        }
//    }
//    return QWidget::eventFilter(target, event);
}

void InquireMemberProfile::keyPressEvent(QKeyEvent *ev)
{
    switch(ev->key())
    {
        case Qt::Key_Escape:
            this->hide();
            break;
    default:
        QWidget::keyPressEvent(ev);
    }
}

//void InquireMemberProfile::mousePressEvent(QMouseEvent *event)
//{
//    if (event->type() == QEvent::MouseButtonPress)
//    {
////        qDebug() << "into1";
//        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
//        if (mouseEvent->button() == Qt::LeftButton)
//        {
////            qDebug() << "into";
////            QPoint GlobalPoint(ui->pushButtonInquireMember->mapToGlobal(QPoint(0, 0)));
////            QPoint GlobalPoint(ui->pushButtonInquireMember->mapToGlobal(QPoint(0, 0)));
//            QPoint point = QCursor::pos();
//            int x = point.x();//该控件在窗体中左上角横坐标
//            int y = point.y();//该控件在窗体中左上角纵坐标
//            qDebug() << x << y;
//        }
//    }
//}

void InquireMemberProfile::displayMemberProfile(const QString &inquireMember)
{
    m_currentTableWidgetList.clear();
    m_currentTableWidgetList = m_sql->InquireMemberProfile();
    ui->tableWidgetMemberProfile->clear();

    setTableWidgetHeaderStyle();

    //判断需要显示的会员资料行数
    int RowCount = 0;
    if (inquireMember != "all")
    {
        for(int i = 0; i < m_currentTableWidgetList.count(); ++i)
        {
            if (!(m_currentTableWidgetList.at(i).get_personID().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personName().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personTel().contains(inquireMember, Qt::CaseInsensitive)))
            {
                continue;
            }
            else
            {
                RowCount++;
            }
        }
        ui->tableWidgetMemberProfile->setRowCount(RowCount);
    }
    else
    {
        ui->tableWidgetMemberProfile->setRowCount(m_currentTableWidgetList.count());
    }

    //显示会员资料
    RowCount = 0;
    for(int i = 0; i < m_currentTableWidgetList.count(); ++i)
    {
        if (inquireMember != "all")
        {
            if (!(m_currentTableWidgetList.at(i).get_personID().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personName().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personTel().contains(inquireMember, Qt::CaseInsensitive)))
            {
                continue;
            }
        }
        QTableWidgetItem *membercardID = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personID());
        QTableWidgetItem *memberName = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personName());
        QTableWidgetItem *gender = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personGender());
        QTableWidgetItem *memberLevel = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_memberLevel());
        QTableWidgetItem *integration = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_integration());
        QTableWidgetItem *discount = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_discount());
        QTableWidgetItem *membercardAmount = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_membercardAmount());
        QTableWidgetItem *totalSpending = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_totalSpending());
        QTableWidgetItem *consumeTimes = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_consumeTimes());
        QTableWidgetItem *handleDate = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_handleDate());
        QTableWidgetItem *tel = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personTel());
        QTableWidgetItem *address = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personAdress());
        QTableWidgetItem *memberRemark = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_memberRemark());

        //设置供货商编号,联系人为居中对齐
        membercardID->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        memberName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        gender->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        memberLevel->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        integration->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        discount->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        membercardAmount->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        totalSpending->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        consumeTimes->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        handleDate->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        tel->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        address->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );
        memberRemark->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );

        ui->tableWidgetMemberProfile->setItem(RowCount, 0, membercardID);
        ui->tableWidgetMemberProfile->setItem(RowCount, 1, memberName);
        ui->tableWidgetMemberProfile->setItem(RowCount, 2, gender);
        ui->tableWidgetMemberProfile->setItem(RowCount, 3, memberLevel);
        ui->tableWidgetMemberProfile->setItem(RowCount, 4, integration);
        ui->tableWidgetMemberProfile->setItem(RowCount, 5, discount);
        ui->tableWidgetMemberProfile->setItem(RowCount, 6, membercardAmount);
        ui->tableWidgetMemberProfile->setItem(RowCount, 7, totalSpending);
        ui->tableWidgetMemberProfile->setItem(RowCount, 8, consumeTimes);
        ui->tableWidgetMemberProfile->setItem(RowCount, 9, handleDate);
        ui->tableWidgetMemberProfile->setItem(RowCount, 10, tel);
        ui->tableWidgetMemberProfile->setItem(RowCount, 11, address);
        ui->tableWidgetMemberProfile->setItem(RowCount, 12, memberRemark);

        RowCount++;
    }

    ui->tableWidgetMemberProfile->setCurrentItem(
                ui->tableWidgetMemberProfile->item(RowCount - 1, 1));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetMemberProfile->resizeColumnsToContents();
    ui->tableWidgetMemberProfile->resizeRowsToContents();

    setTableWidgetColor();
}

void InquireMemberProfile::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetMemberProfile->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetMemberProfile->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetMemberProfile->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252,222,156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void InquireMemberProfile::setTableWidgetHeaderStyle()
{
    QStringList headers;
    headers << tr("会员卡号") << tr("会员姓名") << tr("性别") << tr("会员等级") << tr("积分") << tr("折扣")
            << tr("会员卡额") << tr("消费总额") << tr("消费次数") << tr("办卡日期") << tr("联系电话")
            << tr("联系地址") << tr("备注");

    ui->tableWidgetMemberProfile->setColumnCount(13);
    ui->tableWidgetMemberProfile->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 13; ++i)
    {
        ui->tableWidgetMemberProfile->horizontalHeaderItem(i)->setFont(QFont("Helvetica"));
        ui->tableWidgetMemberProfile->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void InquireMemberProfile::on_lineEditInquireMemberProfile_textChanged(const QString &arg1)
{
    displayMemberProfile(arg1);
}

void InquireMemberProfile::on_pushButtonDisplayAll_clicked()
{
    displayMemberProfile("all");
    ui->tableWidgetMemberProfile->setFocus();
}

void InquireMemberProfile::on_tableWidgetMemberProfile_cellDoubleClicked(int row, int)
{
    emit displayMemberInformation(ui->tableWidgetMemberProfile->item(row, 0)->text());
    this->hide();
}

void InquireMemberProfile::on_pushButtonSelete_clicked()
{
    if (ui->tableWidgetMemberProfile->currentRow() >= 0)
    {
        emit displayMemberInformation(ui->tableWidgetMemberProfile->item(
                                         ui->tableWidgetMemberProfile->currentRow(), 0)->text());
        this->hide();
    }
    else
    {
        ui->lineEditInquireMemberProfile->setFocus();
    }
}
